package com.mycompany.oj.service.Impl;

import com.mycompany.oj.Mapper.Impl.MyQuestionJudgeMapperImpl;
import com.mycompany.oj.Mapper.Impl.MyTeacherMapperImpl;
import com.mycompany.oj.entity.*;
import com.mycompany.oj.service.TeacherService;
import com.mycompany.oj.util.SharedService;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author asus
 */
@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    MyTeacherMapperImpl teacherDao;
    @Autowired
    SharedService sharedservice;
    @Autowired
    MyQuestionJudgeMapperImpl questionMapper;

    @Value("${teacher.teacherPath}")
    private String teacherPath;

    //获取管理员图片
    @Override
    public String getAdminPicure(String username) {
        String account_id = sharedservice.getAccountId(username);
        List<Map<String, Object>> temp = teacherDao.getTeacherPicureMapper(account_id);
        if (temp.isEmpty()) {
            return teacherPath;
        }
        return temp.get(0).get("picture").toString();
    }

    //获取管理员名字
    @Override
    public String getAdminName(String username) {
        String accountId = sharedservice.getAccountId(username);
        String temp = teacherDao.getTeacherNameMapper(accountId).get(0).get("teacher_name").toString();
        return temp;
    }

    //获取每日的提交量
    @Override
    public List<EveryDayInformation> getEverydayPostNumberService() {
        List<Map<String, Object>> temp = teacherDao.getEverydayPostNumberMapper();
        List<EveryDayInformation> list = new ArrayList<>();
        for (Map<String, Object> map : temp) {
            EveryDayInformation everyDayInformation = new EveryDayInformation();
            everyDayInformation.setCount(map.get("count").toString());
            everyDayInformation.setDate(map.get("days").toString());
            list.add(everyDayInformation);
        }
        return list;
    }

    //获取所有学生的情况
    @Override
    public List<AllStudentInformation> getAllStudentInformation(String grade) {
        List<AllStudentInformation> list = new ArrayList<>();
        List<Map<String, Object>> temp = new ArrayList<>();
        if (grade.equals(" ")) {
            temp = teacherDao.getStudentInformation();
        } else {
            temp = teacherDao.doSearch(grade);
        }
        return getStudentInformation(temp, list);
    }

    private List<AllStudentInformation> getStudentInformation(List<Map<String, Object>> temp, List<AllStudentInformation> list) {
        if (!temp.isEmpty()) {
            for (Map<String, Object> map : temp) {
                AllStudentInformation all_Student_Information = new AllStudentInformation();
                all_Student_Information.setRealname(map.get("student_name").toString());
                all_Student_Information.setSchoolid(map.get("schoolid").toString());
                all_Student_Information.setId(map.get("id").toString());
                List<Map<String, Object>> pass_list = sharedservice.check4(teacherDao.getStudentPassCourseNumber(map.get("Account_id").toString()));
                List<Map<String, Object>> post_list = sharedservice.check4(teacherDao.getStudentPostCourseNumber(map.get("Account_id").toString()));
                String pass_number = pass_list.get(0).get("number").toString();
                String post_number = post_list.get(0).get("number").toString();
                all_Student_Information.setPass_number(pass_number);
                all_Student_Information.setPost_number(post_number);
                if (Integer.valueOf(post_number) != 0) {
                    all_Student_Information.setAC_green(String.valueOf((double) (Integer.valueOf(pass_number) / Integer.valueOf(post_number))));
                } else {
                    all_Student_Information.setAC_green("0");
                }
                list.add(all_Student_Information);
            }
            return list;
        } else {
            return list;
        }
    }

    //获取注册账号数目，竞赛题库，
    @Override
    public Adminnumber getNumber() {
        Adminnumber adminnumber = new Adminnumber();
        sharedservice.check2(teacherDao.getStudentCount());
        sharedservice.check2(teacherDao.getQuestionNumber());
        sharedservice.check2(teacherDao.getCourseNumber());
        adminnumber.setStudent_number(teacherDao.getStudentCount().get(0).get("number").toString());
        adminnumber.setQuestion_number(teacherDao.getQuestionNumber().get(0).get("number").toString());
        adminnumber.setCourse_number(teacherDao.getCourseNumber().get(0).get("number").toString());
        return adminnumber;
    }

    //实验题库总情况
    @Override
    public QuestionCourseNumber getSectionInformation() {
        QuestionCourseNumber questionCourseNumber = new QuestionCourseNumber();
        List<Map<String, Object>> courseNum = teacherDao.getAllInformation();
        List<Map<String, Object>> questionNum = teacherDao.getAllInformationT();
        List<Map<String, Object>> total = teacherDao.getTotalInformation();
//        question_Course_Number.setCourse(total.get(0).get("course").toString());
//        question_Course_Number.setSection(total.get(0).get("section").toString());
//        question_Course_Number.setQuestion_course(total.get(0).get("Question_course").toString());
        sharedservice.mapToObject(questionCourseNumber,total.get(0));
        List<Map<String, Object>> temp = new ArrayList<>();
        for (Map<String, Object> map : courseNum) {
            Map<String, Object> e = new HashMap<String, Object>();
            e.put("name", map.get("coursename"));
            e.put("section_number", map.get("number"));
            temp.add(e);
        }
        for (Map<String, Object> map : temp) {
            List<Map<String, Object>> stream = questionNum.stream().filter(x -> x.get("coursename").equals(map.get("name"))).collect(Collectors.toList());
            if (!stream.isEmpty()) {
                String coursename = stream.get(0).get("coursename").toString();
                String number = stream.get(0).get("number").toString();
                String time = stream.get(0).get("time").toString();
                map.put("question_number", number);
                map.put("time", time);
            }
        }
        questionCourseNumber.setSpecific_information(temp);
        return questionCourseNumber;
    }

    //修改题库信息
    @Override
    public boolean doChangeQuestion(String question_name, String question_txt, String question_id) {
        int temp = teacherDao.changeQuestion(question_name, question_txt, question_id);
        return true;
    }

    //课程总结
    @Override
    public CourseSummarize courseSummarize(String coursename) {

        String pass_number, post_number, Course_number, Question_number, time, student_num;
        if (teacherDao.passNumber(coursename).size() != 0) {
            pass_number = teacherDao.passNumber(coursename).get(0).get("number").toString();
        } else {
            pass_number = "0";
        }
        if (teacherDao.postNumber(coursename).size() != 0) {
            post_number = teacherDao.postNumber(coursename).get(0).get("number").toString();
        } else {
            post_number = "0";
        }
        if (teacherDao.sectionNumber(coursename).size() != 0) {
            Course_number = teacherDao.sectionNumber(coursename).get(0).get("number").toString();
        } else {
            Course_number = "0";
        }
        if (teacherDao.sectionQuestionNumber(coursename).size() != 0) {
            Question_number = teacherDao.sectionQuestionNumber(coursename).get(0).get("number").toString();
        } else {
            Question_number = "0";
        }
        if (teacherDao.sectionNumber(coursename).size() != 0) {
            time = teacherDao.sectionNumber(coursename).get(0).get("time").toString();
        } else {
            time = "0";
        }
        if (teacherDao.getStudentCount().size() != 0) {
            student_num = teacherDao.getStudentCount().get(0).get("number").toString();
        } else {
            student_num = "0";
        }
        List<Map<String, Object>> student = teacherDao.everyStudentJindu(coursename);
        String finished_number = finishCourseNumber(student, teacherDao.sectionNumber(coursename), Integer.valueOf(student_num));
        CourseSummarize course_summarize = new CourseSummarize(pass_number, post_number, Course_number, Question_number, time, student_num, finished_number);
        return course_summarize;
    }

    @Override
    public List<QuestionSummary> addCourseInformation(String courseName) {
        List<QuestionSummary> list = new ArrayList<>();
        List<Map<String, Object>> Section_id = teacherDao.getCourseSectionNumId(courseName);
        String number = teacherDao.studentNumber().get(0).get("number").toString();
        Section_id.stream().forEach(x -> {
            List<Map<String, Object>> finish_num = new ArrayList<Map<String, Object>>();
            String endData = teacherDao.getEndData(courseName, x.get("id").toString()).get(0).get("section_end_time").toString();
            String gogal = teacherDao.getEndData(courseName, x.get("id").toString()).get(0).get("section_gogal").toString();
            List<Map<String, Object>> Question_course_id = teacherDao.getQuestionCourseId(courseName, x.get("id").toString());
            int count = 0;
            QuestionSummary question_summary = new QuestionSummary();
            List<String> finished_name = new ArrayList<String>();
            question_summary.setEnd_data(endData);
            for (int i = 1; i <= Integer.parseInt(number); i++) {
                if (!teacherDao.questionFinishedNumber(courseName, String.valueOf(i), x.get("id").toString()).isEmpty()) {
                    String postnumber = teacherDao.questionFinishedNumber(courseName, String.valueOf(i), x.get("id").toString()).get(0).get("number").toString();
                    if (teacherDao.getSectionNum(courseName, x.get("id").toString()).get(0).get("number").equals(postnumber)) {
                        count++;
                        finished_name.add(teacherDao.getStudentName(String.valueOf(i)).get(0).get("student_name").toString());
                    }
                }
            }
            Question_course_id.stream().forEach(y -> {
                int count2 = 0;
                Map<String, Object> e = new HashMap<>();
                for (int i = 1; i <= Integer.parseInt(number); i++) {
                    if (!teacherDao.ifPass(y.get("id").toString(), String.valueOf(i)).isEmpty()) {
                        count2++;
                    }
                }
                e.put(y.get("id").toString(), count2);
                finish_num.add(e);
            });
            question_summary.setFinished_person(String.valueOf(count));
            question_summary.setFinished_name(finished_name);
            question_summary.setPost_number(finish_num);
            list.add(question_summary);
        });
        return list;
    }

    //删除问题
    @Override
    public JsonResult deleteQuestion(String id) {
        int temp = teacherDao.deleteAnswer(id);
        if (sharedservice.check1(String.valueOf(temp)).equals("空")) {
            return new JsonResult(ResultCode.FAIL);
        }
        return new JsonResult(ResultCode.SUCCESS);
    }

    @Override
    public List<ProjectSituation> prjectSituation(String courseName) {
        List<ProjectSituation> list = new ArrayList<>();
        List<Map<String, Object>> Student = teacherDao.studentInformation();
        for (int i = 0; i < Student.size(); i++) {
            ProjectSituation project_Situation = new ProjectSituation();
            project_Situation.setAnswer(judgefinished(courseName, Student.get(i).get("id").toString()));
            project_Situation.setStudent_id(Student.get(i).get("id").toString());
            project_Situation.setStudent_name(Student.get(i).get("student_name").toString());
            project_Situation.setFinish_number(teacherDao.everyStudentFinishedCourseNumber(courseName, Student.get(i).get("Account_id").toString()).get(0).get("number").toString());
            project_Situation.setUndo_number(courseName);
            int do_number = Integer.parseInt(teacherDao.everyStudentDoCourseNumber(courseName, Student.get(i).get("Account_id").toString()).get(0).get("number").toString());
            int total_number = Integer.parseInt(teacherDao.sectionNumber(courseName).get(0).get("number").toString());
            int true_number = Integer.parseInt(teacherDao.everyStudentFinishedCourseNumber(courseName, Student.get(i).get("Account_id").toString()).get(0).get("number").toString());
            project_Situation.setUndo_number(String.valueOf(total_number - do_number));
            project_Situation.setFinish_Green(String.valueOf((double) (true_number / total_number)));
            list.add(project_Situation);
        }

        return list;
    }

    //判断几个实验是否完成
    private List<Map<String, Object>> judgefinished(String course_name, String username) {
        int num;
        //判断各个实验是否完成
        List<Map<String, Object>> temp = new ArrayList<>();
        //各个实验的结果Map
        Map<String, Object> a = new HashMap<>();
        List<Map<String, Object>> number = questionMapper.getZhangjieNumber(course_name);
        if (number.isEmpty()) {
            a.put("0", 0);
            temp.add(a);
            return temp;
        }
        a.put("0", Integer.parseInt(number.get(0).get("number").toString()));
        temp.add(a);

        List<Map<String, Object>> temp1 = questionMapper.haveFinishedNumber(username, course_name);
        List<Map<String, Object>> temp2 = questionMapper.totalNumber(course_name);
        if (temp1.isEmpty()) {
            Map<String, Object> e = new HashMap<>();
            for (Map<String, Object> map : temp2) {
                e.put(map.get("section_num").toString(), "false");
                temp.add(e);
            }

        } else {
            for (int i = 0; i < temp2.size(); i++) {
                Map<String, Object> e = new HashMap<>();
                if (temp1.get(i).get("number") == temp2.get(i).get("section")) {
                    e.put(String.valueOf(i + 1), "success");
                } else {
                    e.put(String.valueOf(i + 1), "debug");
                }
                temp.add(e);
            }
        }
        return temp;
    }

    private String finishCourseNumber(List<Map<String, Object>> student, List<Map<String, Object>> course_number, int student_num) {
        int count = 0;
        List<Map<String, Object>> All_Account = teacherDao.allAccountId();
        for (int i = 0; i < All_Account.size(); i++) {
            int flag = 0;
            int ii = i;
            List<Map<String, Object>> temp = student.stream().filter(x -> x.get("Account_id").toString().equals(All_Account.get(ii).get("id").toString())).collect(Collectors.toList());
            if (!temp.isEmpty() && temp.size() < course_number.size()) {
                int course_num = 0;
                for (Map<String, Object> map : course_number) {
                    if (!temp.get(course_num).get("number").toString().equals(map.get("number").toString())) {
                        flag = 1;
                    }
                }
                if (flag == 0) {
                    count++;
                }
            }
        }

        return String.valueOf(count);
    }

    @Override
    public List<Map<String, Object>> questionNumber() {
        return teacherDao.getKindQuestionNumber();
    }

    //上传实验题目
    @Override
    public JsonResult Importquestion(MultipartFile file) {
        try {
            CommonsMultipartFile cf = (CommonsMultipartFile) file;
            DiskFileItem fi = (DiskFileItem) cf.getFileItem();
            File excel = fi.getStoreLocation();
            String[] split = excel.getName().split("\\.");  //.是特殊字符，需要转义！！！！！
            Workbook wb;
            //根据文件后缀（xls/xlsx）进行判断
            if ("xls".equals(split[1])) {
                FileInputStream fis = new FileInputStream(excel);   //文件流对象
                wb = new HSSFWorkbook(fis);
            } else if ("xlsx".equals(split[1])) {
                wb = new XSSFWorkbook(excel);
            } else {
                System.out.println("文件类型错误!");
                return new JsonResult(ResultCode.FAIL);
            }

            //开始解析
            Sheet sheet = wb.getSheetAt(0);     //读取sheet 0

            int firstRowIndex = sheet.getFirstRowNum() + 1;   //第一行是列名，所以不读
            int lastRowIndex = sheet.getLastRowNum();
            for (int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex++) {   //遍历行
                QuestionUpload question_upload = new QuestionUpload();

                Row row = sheet.getRow(rIndex);
                if (row != null) {

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JsonResult(ResultCode.SUCCESS);
    }

    //上传课程题目
    @Override
    public JsonResult ImportCoursequestion(MultipartFile file) {
        try {
            CommonsMultipartFile cf = (CommonsMultipartFile) file;
            DiskFileItem fi = (DiskFileItem) cf.getFileItem();
            File excel = fi.getStoreLocation();
            String[] split = excel.getName().split("\\.");  //.是特殊字符，需要转义！！！！！
            Workbook wb;
            //根据文件后缀（xls/xlsx）进行判断
            if ("xls".equals(split[1])) {
                FileInputStream fis = new FileInputStream(excel);   //文件流对象
                wb = new HSSFWorkbook(fis);
            } else if ("xlsx".equals(split[1])) {
                wb = new XSSFWorkbook(excel);
            } else {
                System.out.println("文件类型错误!");
                return new JsonResult(ResultCode.FAIL);
            }

            //开始解析
            Sheet sheet = wb.getSheetAt(0);     //读取sheet 0

            int firstRowIndex = sheet.getFirstRowNum() + 1;   //第一行是列名，所以不读
            int lastRowIndex = sheet.getLastRowNum();
            for (int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex++) {   //遍历行
                QuestionUpload question_upload = new QuestionUpload();

                Row row = sheet.getRow(rIndex);
                if (row != null) {

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JsonResult(ResultCode.SUCCESS);
    }

    //获取题目
    public List<Map<String, Object>> getAllCourse() {
        List<Map<String, Object>> temp = teacherDao.getCourse();
        return temp;
    }

    //网站整体情况
    public JsonResult doWebService() {
        int temp1 = Integer.parseInt(teacherDao.getYesterdayCourseNum().get(0).get("number").toString());
        int temp2 = Integer.parseInt(teacherDao.getYesterdayQuestionNum().get(0).get("number").toString());
        WebTotalPosition web_total_Position = new WebTotalPosition();
        web_total_Position.setYesterday_post(temp2 + temp1);

        int temp3 = Integer.parseInt(teacherDao.getYesterdayCourseTrueNum().get(0).get("number").toString());
        int temp4 = Integer.parseInt(teacherDao.getYesterdayQuestionTrueNum().get(0).get("number").toString());
        web_total_Position.setYesterday_pass(temp3 + temp4);

        return new JsonResult(ResultCode.SUCCESS, web_total_Position);
    }

    //题库管理
    public JsonResult doGuranteeService(String course_name) {

        List<ZhangjieInformation> list = new ArrayList<>();
        list = sectionInformation(course_name);

        return new JsonResult(ResultCode.SUCCESS, list);
    }

    private List<ZhangjieInformation> sectionInformation(String course_name) {
        List<ZhangjieInformation> temp = new ArrayList<>();
        //本课程题目名字id
        List<Map<String, Object>> All_Question_Information = teacherDao.allQuestion(course_name);
        //所有章节
        List<Map<String, Object>> All_Section_Information = questionMapper.getZhangjieInformation(course_name);
        if (!All_Section_Information.isEmpty()) {
            //章节简介
            All_Section_Information.stream().forEach((Map<String, Object> x) -> {
                temp.add(new ZhangjieInformation(x.get("section_name").toString(), x.get("section_num").toString(), x.get("section_end_time").toString(), x.get("section_gogal").toString(), x.get("section_num").toString()));
            });

            if (!All_Question_Information.isEmpty()) {
                //temp1.size()为章节数目
                for (int i = 0; i < All_Section_Information.size(); i++) {
                    String tempi = All_Section_Information.get(i).get("section_num").toString();
                    List<Map<String, String>> zhangjie_Question = new ArrayList<>();
                    for (Map<String, Object> map : All_Question_Information.stream().filter(x -> x.get("section_num").toString().equals(tempi)).collect(Collectors.toList())) {
                        Map<String, String> e = new HashMap<>();
                        e = addPpNumber(e, map.get("id").toString());
                        e.put("id", map.get("id").toString());
                        e.put("name", map.get("question_name").toString());
                        zhangjie_Question.add(e);
                    }
                    temp.get(i).init_zhangjie_question(zhangjie_Question);
                }
                return temp;
            } else {
                //该章节没有题目
                return temp;
            }
        } else {
            //该课程没有章节
            ZhangjieInformation e = new ZhangjieInformation("0", "0", "0", "0", "0");
            temp.add(e);
            return temp;
        }
    }

    private Map<String, String> addPpNumber(Map<String, String> temp, String id) {
        List<Map<String, Object>> number = teacherDao.everyCoursePostNumber(id);
        if (number.isEmpty()) {
            Map<String, Object> a = new HashMap();
            a.put("number", 0);
            number.add(a);
        }
        temp.put("number", number.get(0).get("number").toString());
        return temp;
    }

    //    实验信息管理
    public JsonResult getCourseStudent(String course_name) {
        List<CourseEveryStudent> temp = new ArrayList();
        List<Map<String, Object>> schoolid = teacherDao.getSchoolid();
        //总信息
        List<Map<String, Object>> temp1 = teacherDao.getCoursePosition(course_name);
        //章节数
        List<Map<String, Object>> Section_id = teacherDao.getNewCourseId(course_name);
        //这一章所有的题目量
        List<Map<String, Object>> all_number = teacherDao.getAllInformationPlus(course_name);
        //每章题目量
        List<Map<String, Object>> section_number = teacherDao.getSectionNumber(course_name);
        schoolid.stream().forEach(x -> {
            CourseEveryStudent course_Every_Student = new CourseEveryStudent();
            course_Every_Student.setSchoolid(x.get("schoolid").toString());
            List<Map<String, Object>> experiment = new ArrayList();
            List<Map<String, Object>> filtemp1 = temp1.stream().filter(y -> y.get("schoolid").toString().equals(x.get("schoolid").toString())).collect(Collectors.toList());

            if (filtemp1.isEmpty()) {
                course_Every_Student.setStudent_name(teacherDao.getStudentName(x.get("schoolid").toString()).get(0).get("student_name").toString());
                Section_id.stream().forEach(z -> {
                    Map<String, Object> e = new HashMap();
                    e.put(z.get("id").toString(), "未完成");
                    experiment.add(e);
                });
                course_Every_Student.setExperiment(experiment);
                course_Every_Student.setFinish_number(0);
                course_Every_Student.setAC(0);
                course_Every_Student.setFinish_number(0);
                course_Every_Student.setUnfinished_number(Integer.parseInt(all_number.get(0).get("number").toString()));
            } else {
                course_Every_Student.setStudent_name(filtemp1.get(0).get("student_name").toString());
                Section_id.stream().forEach(y -> {
                    List<Map<String, Object>> filtfiltemp1 = filtemp1.stream().filter(z -> z.get("Section_num").toString().equals(y.get("id").toString())).collect(Collectors.toList());
                    if (!filtfiltemp1.isEmpty() && String.valueOf(filtfiltemp1.size()).equals(section_number.stream().filter(z -> z.get("Section_num").toString().equals(y.get("id").toString())).collect(Collectors.toList()).get(0).get("number").toString())) {
                        int num = filtfiltemp1.size();

                        Map<String, Object> e = new HashMap();
                        e.put(y.get("id").toString(), "完成");
                        experiment.add(e);
                    } else {
                        Map<String, Object> e = new HashMap();
                        e.put(y.get("id").toString(), "未完成");
                        experiment.add(e);
                    }
                });
                course_Every_Student.setExperiment(experiment);
                int finnum = 0;
                for (int i = 0; i < Section_id.size(); i++) {
                    Map<String, Object> e = Section_id.get(i);
                    List<Map<String, Object>> filtfiltemp1 = filtemp1.stream().filter(z -> z.get("Section_num").toString().equals(e.get("id").toString())).collect(Collectors.toList());
                    finnum += filtfiltemp1.size();
                }
                course_Every_Student.setFinish_number(finnum);
                course_Every_Student.setUnfinished_number(Integer.parseInt(all_number.get(0).get("number").toString()) - finnum);
                course_Every_Student.setAC(finnum / Integer.parseInt(all_number.get(0).get("number").toString()));
            }
            temp.add(course_Every_Student);
        });
        return new JsonResult(ResultCode.SUCCESS, temp);
    }

    //删除课程
    public JsonResult deleteCourse(String course) {
        List<Map<String, Object>> temp1 = teacherDao.getCourseIdByName(course);
        List<Map<String, Object>> temp2 = teacherDao.getSectionId(temp1.get(0).get("id").toString());
        if (temp2.isEmpty()) {
            teacherDao.delelteCourseId(temp1.get(0).get("id").toString());
            return new JsonResult(ResultCode.SUCCESS);
        }
        temp2.stream().forEach(x -> {
            List<Map<String, Object>> temp3 = teacherDao.getQuestionCourseId(x.get("id").toString());
            if (!temp3.isEmpty()) {

                temp3.stream().forEach(y -> {
                    List<Map<String, Object>> temp4 = teacherDao.getQuestionCourseRegisterId(y.get("id").toString());
                    if (!temp4.isEmpty()) {
                        temp4.stream().forEach(z -> teacherDao.delelteQuestionRegisterId(z.get("id").toString()));
                    }
                });
                temp3.stream().forEach(y -> teacherDao.delelteQuestionCourseId(y.get("id").toString()));
            }
        });
        temp2.stream().forEach(x -> teacherDao.delelteSectionId(x.get("id").toString()));
        teacherDao.delelteCourseId(temp1.get(0).get("id").toString());
        return new JsonResult(ResultCode.SUCCESS);
    }

    //搜索学生
    public JsonResult doSearchStudent(String grade, String name, String schoolid) {
        List<AllStudentInformation> list = new ArrayList<>();
        if (grade.equals(" ")) {
            List<Map<String, Object>> temp = teacherDao.getStudentInformation();
            list = getStudentInformation(temp, list);
        } //只有年级
        else if (name.equals("") && schoolid.equals("")) {
            List<Map<String, Object>> temp = teacherDao.doSearch(grade);
            list = getStudentInformation(temp, list);
            //只有年级和姓名
        } else if (schoolid.equals("")) {
            List<Map<String, Object>> temp = teacherDao.doSearchNameAndGrad(name, grade);
            list = getStudentInformation(temp, list);
            //只有学号和年级
        } else if (name.equals("")) {
            List<Map<String, Object>> temp = teacherDao.doSearchGradeandschoolid(grade, schoolid);
            list = getStudentInformation(temp, list);
        } else {
            List<Map<String, Object>> temp = teacherDao.doSearchNameAndSchoolid(name, schoolid);
            list = getStudentInformation(temp, list);
        }

        if (list.isEmpty()) {
            return new JsonResult(ResultCode.FAIL);
        } else {
            return new JsonResult(ResultCode.SUCCESS, list);
        }
    }

    //删除学生
    public JsonResult deleteStudent(String student_id) {
        teacherDao.deleteStudentRegister(student_id);
        return new JsonResult(ResultCode.SUCCESS);
    }

    //修改实验
    public JsonResult changeCourseQuestion(String course_name, String id, String name, String gogal, String end_data) {
        teacherDao.doChangeSection(course_name, id, name, gogal, end_data);
        return new JsonResult(ResultCode.SUCCESS);
    }

    //停用问题
    public JsonResult stopQuestion(String id) {
        teacherDao.doStopQuestion(id);
        return new JsonResult(ResultCode.SUCCESS);
    }

    //停用章节
    public JsonResult stopSection(String course, String section_num) {
        teacherDao.doStopSection(course, section_num);
        return new JsonResult(ResultCode.SUCCESS);
    }

    //停用实验
    public JsonResult stopCourse(String id) {
        teacherDao.doStopCourse(id);
        return new JsonResult(ResultCode.SUCCESS);
    }

    //启用问题
    public JsonResult startQuestion(String id) {
        teacherDao.doStartQuestion(id);
        return new JsonResult(ResultCode.SUCCESS);
    }

    //启用章节
    public JsonResult startSection(String course, String section_num) {
        teacherDao.do_start_section(course, section_num);
        return new JsonResult(ResultCode.SUCCESS);
    }

    //启用实验
    public JsonResult startCourse(String id) {
        teacherDao.doStartCourse(id);
        return new JsonResult(ResultCode.SUCCESS);
    }

    //获取停用实验
    public JsonResult getStopCourse() {
        List<Map<String, Object>> temp = teacherDao.doGetStopCourse();
        return new JsonResult(ResultCode.SUCCESS, temp);
    }

    //获取停用章节
    public JsonResult getStopSection() {
        List<Map<String, Object>> temp = teacherDao.doGetStopSection();
        return new JsonResult(ResultCode.SUCCESS, temp);
    }

    //获取停用问题
    public JsonResult getStopQuestion() {
        List<Map<String, Object>> temp = teacherDao.doGetStopQuestion();
        return new JsonResult(ResultCode.SUCCESS, temp);
    }

    //添加章节
    public JsonResult addSection(String course_name, String section_name, String section_num, String section_gogal, String section_end_data) {
        String[] temp = teacherDao.addSection(course_name, section_name, section_num, section_gogal, section_end_data);
        return new JsonResult(ResultCode.SUCCESS);
    }

    //添加题目
    public JsonResult addQuestion(String coursename, String section_num, String question_name, String question_text, MultipartFile[] question_data, String standard_answer) {


        String[] temp = teacherDao.addQuestion(coursename, section_num, question_name, question_text, "", standard_answer);
        return new JsonResult(ResultCode.SUCCESS);
    }
}
